package com.hdjk.view.network;

import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.FundDetailCallBack;
import com.hdjk.view.callback.FundListCallBack;
import com.hdjk.view.callback.GetFundPerformanceCallBack;
import com.hdjk.view.callback.GetFundTrendPerformanceCallBack;
import com.hdjk.view.callback.GetPerformanceCallBack;
import com.hdjk.view.callback.GetRedeemSellFeeCallBack;
import com.hdjk.view.callback.GetRedeemTransactionFeeCallBack;
import com.hdjk.view.config.Constant;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;

/**
 * Created by sxy on 2017/11/2.
 */

public class FundNetWork {

    public static void getFundList(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_FUND_LIST)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new FundListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getFundDetail(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_FUND_DETAIL)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new FundDetailCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getFundPerfromance(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_FUND_PERFORMANCE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetFundPerformanceCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getFundTypePerfromance(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_FIND_FUND_PERFORMANCE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetFundTrendPerformanceCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getMarketPerfromance(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_MARKET_PERFORMANCE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetPerformanceCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getRedeemTransactionFee(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_REDEEM_TRANSACTION_FEE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetRedeemTransactionFeeCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void getRedeemSellFee(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_REDEEM_SELL_FEE)
                    .addParams("paramList", jsonInfo)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetRedeemSellFeeCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
}
